package ym.ecrm.core.dto.systemManagement;

import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

import org.apache.commons.beanutils.BeanUtils;

import ym.ecrm.core.domain.systemManagement.Role;
import ym.ecrm.core.domain.systemManagement.User;

public class UserDTO {

	private Long userId;
	private String userName;
	private String password;
	private String realName;
	private String userCode;
	private String state;
	private String phone;
	private String mobilePhone;
	private String emailAddress;
	private Boolean enabled;
	private Boolean builtIn;
	private String note;
	
	private String department;
	
	private List<UserHasManyRoleDTO> role = new LinkedList();
	
	public static void entity2dto(User user, UserDTO userDTO){
		try {
			BeanUtils.copyProperties(userDTO, user);
			userDTO.setUserId(user.getId());
			Set<Role> set = user.getRoles();
			Iterator<Role> iterator = set.iterator();
			List<UserHasManyRoleDTO> list = new LinkedList();
			while (iterator.hasNext()) {
				Role role = (Role) iterator.next();
				UserHasManyRoleDTO userHasManyRoleDTO = new UserHasManyRoleDTO();
				BeanUtils.copyProperties(userHasManyRoleDTO, role);
				userHasManyRoleDTO.setRoleId(role.getId());
				userHasManyRoleDTO.setUserId(user.getId());
				list.add(userHasManyRoleDTO);
			}
			userDTO.setRole(list);
			if(user.getOrganization() != null)
				userDTO.setDepartment(user.getOrganization().getName());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
		catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	public static void dto2entity(UserDTO userDTO, User user){
		try {
			BeanUtils.copyProperties(user, userDTO);
			user.setId(userDTO.getUserId());
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}
	public Long getUserId() {
		return userId;
	}
	public void setUserId(Long userId) {
		this.userId = userId;
	}
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getRealName() {
		return realName;
	}
	public void setRealName(String realName) {
		this.realName = realName;
	}
	public String getUserCode() {
		return userCode;
	}
	public void setUserCode(String userCode) {
		this.userCode = userCode;
	}
	public String getState() {
		return state;
	}
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getMobilePhone() {
		return mobilePhone;
	}

	public void setMobilePhone(String mobilePhone) {
		this.mobilePhone = mobilePhone;
	}

	public Boolean getEnabled() {
		return enabled;
	}

	public void setEnabled(Boolean enabled) {
		this.enabled = enabled;
	}

	public Boolean getBuiltIn() {
		return builtIn;
	}

	public void setBuiltIn(Boolean builtIn) {
		this.builtIn = builtIn;
	}

	public void setState(String state) {
		this.state = state;
	}
	public String getEmailAddress() {
		return emailAddress;
	}
	public void setEmailAddress(String emailAddress) {
		this.emailAddress = emailAddress;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}

	public String getNote() {
		return note;
	}

	public void setNote(String note) {
		this.note = note;
	}

	public List<UserHasManyRoleDTO> getRole() {
		return role;
	}

	public void setRole(List<UserHasManyRoleDTO> role) {
		this.role = role;
	}
	
}
